﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

 class ComplexNumber
 {
     #region fields
     public double Real
     {
         get;
         private set;
     }

     public double Imaginary
     {
         get;
         private set;
     }
     #endregion

     #region constructors
     public ComplexNumber(double r, double i)
     {
         this.Real = r;
         this.Imaginary = i;
     }
     public ComplexNumber(double real)
     {
         this.Real = real;
         this.Imaginary = 0;
     }
     #endregion

     #region PublicMethods
     public static ComplexNumber Sum(ComplexNumber a, ComplexNumber b)
     {
         double real = a.Real + b.Real;
         double im = a.Imaginary + b.Imaginary;

         return new ComplexNumber(real, im);
     }

     public static ComplexNumber operator +(ComplexNumber a, ComplexNumber b)
     {
         return Sum(a, b);
     }

     public static ComplexNumber Subtract(ComplexNumber a, ComplexNumber b)
     {
         double real = a.Real - b.Real;
         double im = a.Imaginary - b.Imaginary;

         return new ComplexNumber(real, im);
     }

     public static ComplexNumber operator -(ComplexNumber a, ComplexNumber b)
     {
         return Subtract(a, b);
     }

     public static ComplexNumber Multiply(ComplexNumber a, ComplexNumber b)
     {
         double real = a.Real * b.Real - a.Imaginary * b.Imaginary;
         double im = a.Imaginary * b.Real + a.Real * b.Imaginary;

         return new ComplexNumber(real, im);
     }

     public static ComplexNumber operator *(ComplexNumber a, ComplexNumber b)
     {
         return Multiply(a, b);
     }

     public static ComplexNumber Divide(ComplexNumber c1, ComplexNumber c2)
     {
         double a = c1.Real;
         double b = c1.Imaginary;
         double c = c2.Real;
         double d = c2.Imaginary;

         double real = (a * c + b * d) / (c * c + d * d);
         double im = (b * c - a * d) / (c * c + d * d);

         return new ComplexNumber(real, im);
     }

     public static ComplexNumber operator /(ComplexNumber c1, ComplexNumber c2)
     {
         return Divide(c1, c2);
     }

     public static bool operator ==(ComplexNumber a, ComplexNumber b)
     {
         return a.Real == b.Real && a.Imaginary == b.Imaginary;
     }
     
     public static bool operator !=(ComplexNumber a, ComplexNumber b)
     {
         return a.Real == b.Real && a.Imaginary == b.Imaginary;
     }

     public override int GetHashCode()
     {
         return this.Real.GetHashCode() * this.Imaginary.GetHashCode();
     }
     
     public override bool Equals(object obj)
     {
         return this == (ComplexNumber)obj;
     }
     
     public override string ToString()
     {
         return "(" + Real + ", " + Imaginary + "i" + ")";
     }

     public static explicit operator double(ComplexNumber c)
     {
         return c.Real;
     }

     public static implicit operator ComplexNumber(double a)
     {
         return new ComplexNumber(a);
     }

#endregion

     public  double this[int index]
     {
         get 
         {
             if(index < 0 || index >1) throw new ArgumentException();
             else
             {
                 if(index == 0) return this.Real;
                 else return this.Imaginary;
             }
         }
         set
         {
             if(index < 0 || index >1) throw new ArgumentException();
             else
             {
                if(index == 0)  this.Real = value;
                else  this.Imaginary = value;
             }
         }
     }

     static void Main(string[] args)
     {
         ComplexNumber c1 = new ComplexNumber(4, 5);
         c1[0] = 6;
         c1[1] = 5;

         Console.WriteLine(c1);
     }
 }
